package com.deluxelx.rmo.essay.domain.rule.repository.impl;

import com.deluxelx.rmo.common.constants.TreeConstants;
import com.deluxelx.rmo.essay.domain.rule.repository.IRuleTreeRepository;
import com.deluxelx.rmo.essay.mapper.RuleTreeMapper;
import com.deluxelx.rmo.essay.mapper.RuleTreeNodeLineMapper;
import com.deluxelx.rmo.essay.mapper.RuleTreeNodeMapper;
import com.deluxelx.rmo.essay.model.aggregates.TreeRuleRich;
import com.deluxelx.rmo.essay.model.bo.TreeNode;
import com.deluxelx.rmo.essay.model.bo.TreeNodeLine;
import com.deluxelx.rmo.essay.model.bo.TreeRoot;
import com.deluxelx.rmo.essay.model.po.RuleTree;
import com.deluxelx.rmo.essay.model.po.RuleTreeNode;
import com.deluxelx.rmo.essay.model.po.RuleTreeNodeLine;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class RuleTreeRepository implements IRuleTreeRepository {

    @Resource
    private RuleTreeMapper ruleTreeMapper;

    @Resource
    private RuleTreeNodeMapper ruleTreeNodeMapper;

    @Resource
    private RuleTreeNodeLineMapper ruleTreeNodeLineMapper;

    @Override
    public TreeRuleRich queryTreeRuleRich(Long treeId) {
        // 1. 整棵规则树的整体
        RuleTree ruleTree = ruleTreeMapper.queryRuleTreeByTreeId(treeId);
        TreeRoot treeRoot = new TreeRoot();
        treeRoot.setTreeId(ruleTree.getId());
        treeRoot.setTreeRootNodeId(ruleTree.getTreeRootNodeId());
        treeRoot.setTreeName(ruleTree.getTreeName());
        // 2. 树节点 -> 树连接线
        Map<Long, TreeNode> treeNodeMap = new HashMap<>();
        // 2.1 获取所有树节点
        List<RuleTreeNode> ruleTreeNodeList = ruleTreeNodeMapper.queryRuleTreeNodeList(treeId);
        ruleTreeNodeList.stream().peek(ruleTreeNode -> {
            // 3. 得到 treeNode 的连接线集合
            List<TreeNodeLine> treeNodeLineList = new ArrayList<>();
            // TODO 如果节点为（树茎），则表明其有连接线。找出与 treeNode 相连的所有连线
            if (TreeConstants.STEM.equals(ruleTreeNode.getNodeType())) {
                List<RuleTreeNodeLine> ruleTreeNodeLineList = ruleTreeNodeLineMapper.queryRuleTreeNodeLineList(treeId, ruleTreeNode.getId());
                ruleTreeNodeLineList.stream().peek(ruleTreeNodeLine -> {
                    TreeNodeLine treeNodeLine = new TreeNodeLine();
                    BeanUtils.copyProperties(ruleTreeNodeLine, treeNodeLine);
                    treeNodeLineList.add(treeNodeLine);
                }).forEach(n -> {});
            }
            // TODO 找到（树茎）节点的所有连接线后，new 一个 TreeNode 节点。Mapper 得到的是 ruleTreeNode 节点
            TreeNode treeNode = new TreeNode();
            treeNode.setTreeId(ruleTreeNode.getTreeId());
            treeNode.setTreeNodeId(ruleTreeNode.getId());
            treeNode.setNodeType(ruleTreeNode.getNodeType());
            treeNode.setNodeValue(ruleTreeNode.getNodeValue());
            treeNode.setRuleKey(ruleTreeNode.getRuleKey());
            treeNode.setRuleDesc(ruleTreeNode.getRuleDesc());
            treeNode.setTreeNodeLineInfoList(treeNodeLineList);

            treeNodeMap.put(treeNode.getTreeNodeId(), treeNode);
        }).forEach(n -> {});
        // 4. 获得一个 treeRuleRich 类
        TreeRuleRich treeRuleRich = new TreeRuleRich();
        treeRuleRich.setTreeRoot(treeRoot);
        treeRuleRich.setTreeNodeMap(treeNodeMap);
        return treeRuleRich;
    }
}
